package com.bai.serviceOA.Controller;


import com.bai.common.Result;
import com.bai.model.system.SysMenu;
import com.bai.model.vo.AssginMenuVo;
import com.bai.model.vo.AssginRoleVo;
import com.bai.serviceOA.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author bai
 * @since 2023-05-11
 */
@Api(tags = "菜单管理接口")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;


    /**
     * 查询所有菜单和角色分配的菜单
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.list')and hasAuthority('bnt.sysMenu.list')")
    @ApiOperation("查询所有菜单和角色分配的菜单")
    @GetMapping("toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId) {
        List<SysMenu> list = sysMenuService.findMenu(roleId);
        return Result.ok(list);
    }

    /**
     * 角色分配菜单
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.update')and hasAuthority('bnt.sysMenu.update')")

    @ApiOperation("角色分配菜单")
    @PostMapping("/doAssgin")
    public Result doAssgin(@RequestBody AssginMenuVo assginMenuVo){
       sysMenuService.doAssgin(assginMenuVo);
       return Result.ok();
    }


    /**
     * 菜单列表
     *
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    @ApiOperation("菜单列表")
    @GetMapping("findNodes")
    public Result findNodes() {
        List<SysMenu> list = sysMenuService.findNodes();
        return Result.ok(list);
    }


    /**
     * 新增菜单
     *
     * @param permission
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysMenu.add')")
    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    public Result save(@RequestBody SysMenu permission) {
        sysMenuService.save(permission);
        return Result.ok();
    }


    /**
     * 修改惨淡
     *
     * @param permission
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    public Result update(@RequestBody SysMenu permission) {
        sysMenuService.updateById(permission);
        return Result.ok();
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        sysMenuService.removeMwnuById(id);
        return Result.ok();
    }

}

